package com.jied.duodian.service.Impl;

import com.jied.duodian.dao.DdProductDAO;
import com.jied.duodian.entity.DdProduct;
import com.jied.duodian.entity.DdProductExample;
import com.jied.duodian.entity.Dutity.Dupaixu;
import com.jied.duodian.entity.Dutity.Page;
import com.jied.duodian.service.DdProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/10/29
 * WWW https://github.com/j6l
 */
@Service
@Slf4j
@Transactional
public class DdProductServiceImpl implements DdProductService {
    @Autowired
    DdProductDAO ddProductDAO;


    @Override
    @Cacheable(cacheNames = "qt", keyGenerator = "keyGenerator")
    public DdProduct getProductByid(Integer id) {
        DdProduct ddProduct = ddProductDAO.selectByPrimaryKey( id );
        return ddProduct;
    }

    @Override
    public Integer updataProduct(DdProduct product) {
        return ddProductDAO.updateByPrimaryKeySelective( product );
    }

    @Override
    public Integer delProductByid(Integer hh) {
        return ddProductDAO.deleteByPrimaryKey( hh );
    }

    @Override
    public List<DdProduct> getProductByActivity(String lei) {
        DdProductExample example = new DdProductExample();
        DdProductExample.Criteria criteria = example.createCriteria();

        criteria.andVerifyStatusEqualTo( 1 );
        criteria.andPublishStatusEqualTo( 1 );
        criteria.andCategoryNameEqualTo( lei );
        criteria.andActivityIsNotNull();
        example.setOrderByClause( "activity" );
        example.setLimit( 5 );
        return ddProductDAO.selectByExample( example );
    }

    @Override
    public Integer addProduct(DdProduct ddProduct) {
        return ddProductDAO.insertSelective( ddProduct );
    }

    @Override
    public Page getallpageList(DdProduct product, Page page, Integer mc) {
        DdProductExample example = new DdProductExample();
        DdProductExample.Criteria criteria = example.createCriteria();
        if (mc == 0) criteria.andIdIsNotNull();
        if (mc == 1) criteria.andProductSnLike( "%" + product.getProductSn() + "%" );
        if (mc == 2) criteria.andNameLike( "%" + product.getName() + "%" );
        return getPage( page, example );
    }

    /**
     * 压缩重复代码
     *
     * @param page
     * @param example
     * @return
     */
    public Page getPage(Page page, DdProductExample example) {
        long count = ddProductDAO.countByExample( example );
        page.setTotal( count );
        example.setLimit( page.getSize() );
        example.setOffset( Long.valueOf( page.getSize() * (page.getPageindex() - 1) ) );
        List<DdProduct> list = ddProductDAO.selectByExample( example );
        page.setContent( list );
        return page;
    }


    @Override
    public Page getallpageListBysh(DdProduct product, Page page) {
        DdProductExample example = new DdProductExample();
        DdProductExample.Criteria criteria = example.createCriteria();
        criteria.andVerifyStatusEqualTo( 0 );
        return getPage( page, example );
    }

    @Override
    public Page getpaixu(Dupaixu dupaixu, Integer page) {
        // log.info( "一一一一一  dupaixu  {}   page  {}", dupaixu, page );
        //创建基础类
        Page page1 = new Page();
        page1.setPageindex( page );
        DdProductExample example = new DdProductExample();
        DdProductExample.Criteria criteria = example.createCriteria();
        DdProductExample.Criteria criteria1 = example.createCriteria();

        //排除未上架 未审核商品
        criteria.andVerifyStatusEqualTo( 1 );
        criteria1.andVerifyStatusEqualTo( 1 );
        criteria.andPublishStatusEqualTo( 1 );
        criteria1.andPublishStatusEqualTo( 1 );

        //判断是否需要品牌
        if (!dupaixu.getBrandname().equals( "0" )) {
            criteria.andBrandNameEqualTo( dupaixu.getBrandname() );
            criteria1.andBrandNameEqualTo( dupaixu.getBrandname() );
        }
        ;
        //是否需要种类
        if (!dupaixu.getLei().equals( "0" )) {
            criteria.andCategoryNameEqualTo( dupaixu.getLei() );
            criteria1.andCategoryNameEqualTo( dupaixu.getLei() );
        }
        if (dupaixu.getSproduct() != null) {
            //名字模糊查询
            criteria.andNameLike( "%" + dupaixu.getSproduct() + "%" );
            //描述模糊查询
            criteria1.andDescriptionLike( "%" + dupaixu.getSproduct() + "%" );
            example.or( criteria1 );
        }


        //符合内容有数量
        long count = ddProductDAO.countByExample( example );
        page1.setTotal( count );
        example.setLimit( 12 );
        example.setOffset( Long.valueOf( 12 * (page - 1) ) );

        //判定排序
        if (dupaixu.getPaixu() == 1) example.setOrderByClause( "money DESC" );
        if (dupaixu.getPaixu() == 2) example.setOrderByClause( "money ASC" );
        if (dupaixu.getPaixu() == 3) example.setOrderByClause( "activity DESC" );
        if (dupaixu.getPaixu() == 4) example.setOrderByClause( "activity ASC" );
        List<DdProduct> list = ddProductDAO.selectByExample( example );
        page1.setContent( list );
        // log.info( "一一一一一page  {}", dupaixu, page );
        return page1;
    }


    @Override
    public Long getProductCuntDsh() {
        DdProductExample example = new DdProductExample();
        DdProductExample.Criteria criteria = example.createCriteria();
        criteria.andVerifyStatusEqualTo( 0 );
        long l = ddProductDAO.countByExample( example );
        return l;
    }

    @Override
    public Long getProductCuntDsj() {
        DdProductExample example = new DdProductExample();
        DdProductExample.Criteria criteria = example.createCriteria();
        criteria.andPublishStatusEqualTo( 0 );
        long l = ddProductDAO.countByExample( example );
        return l;
    }
}
